package com.styytech.dadabao.test.camera;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.styytech.dadabao.R;
import com.styytech.dadabao.widge.RoundedImage.RoundedImage;
import com.styytech.dadabao.widge.dialog.CameraDialog;
import com.styytech.dadabao.widge.headportrait.Bimp;
import com.styytech.dadabao.widge.headportrait.BitmapCache;
import com.styytech.dadabao.widge.headportrait.SportsUtilities;
import com.styytech.dadabao.widge.headportrait.Tools;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Created by User on 2015/8/19.
 */
public class CameraMain extends Activity implements OnClickListener {
    private static final String FACE_PATH = SportsUtilities.DOWNLOAD_SAVE_PATH
            + "/faceImage.jpg";
    private RoundedImage round_img;
    private CameraDialog cameradialog;
    private BitmapDrawable drawable;
    private static final String IMAGE_FILE_NAME = "faceImage.jpg";
    /* 请求码 */
    private static final int IMAGE_REQUEST_CODE = 0;
    private static final int CAMERA_REQUEST_CODE = 1;
    private static final int RESULT_REQUEST_CODE = 2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        round_img=(RoundedImage)findViewById(R.id.round_imgs);
        Log.e("sadasd","round_img----------------"+round_img);
        findViewById(R.id.btn_new).setOnClickListener(this);
        round_img.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.round_imgs:
                showDialog();
                break;
            case R.id.btn_new:
                //startActivity(new Intent(this,NewActivity.class));
                break;
            default:
                break;
        }
    }
    /**
     * 显示选择对话框
     */
    private void showDialog() {
        cameradialog=new CameraDialog(this);
        cameradialog.setCameraDialogCallback(cameraDialogcallback);
        cameradialog.setPhotoDialogCallback(photoDialogcallback);
        cameradialog.show();
    }
    /**
     * 设置mydialog需要处理的事情
     */
    CameraDialog.Dialogcallback cameraDialogcallback = new CameraDialog.Dialogcallback() {
        @Override
        public void dialogdo() {
            cameradialog.dismiss();
            //照相
            //调用系统相机
            Intent intentFromCapture = new Intent(
                    MediaStore.ACTION_IMAGE_CAPTURE);
            // 判断存储卡是否可以用，可用进行存储
            if (Tools.hasSdcard()) {
                //MediaStore.EXTRA_OUTPUT----自定义裁剪输出位置
                intentFromCapture.putExtra(
                        MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(new File(Environment
                                .getExternalStorageDirectory(),
                                IMAGE_FILE_NAME)));
            }

            startActivityForResult(intentFromCapture,
                    CAMERA_REQUEST_CODE);
        }
    };
    CameraDialog.Dialogcallback photoDialogcallback = new CameraDialog.Dialogcallback() {
        @Override
        public void dialogdo() {
            cameradialog.dismiss();
            //相册
            Intent intentFromGallery = new Intent(
                    Intent.ACTION_PICK, null);
            intentFromGallery
                    .setDataAndType(
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                            "image/*");
            startActivityForResult(intentFromGallery,
                    IMAGE_REQUEST_CODE);
        }
    };
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // 结果码不等于取消时候
        if (resultCode != RESULT_CANCELED) {

            switch (requestCode) {
                case IMAGE_REQUEST_CODE:
                    startPhotoZoom(data.getData());
                    //getOn(data.getData());
                    break;
                case CAMERA_REQUEST_CODE:
                    if (Tools.hasSdcard()) {
                        File tempFile = new File(
                                Environment.getExternalStorageDirectory()
                                        + File.separator + IMAGE_FILE_NAME);
                        //裁剪
                        startPhotoZoom(Uri.fromFile(tempFile));
                        //不裁剪
                        Log.e("asdasd","tempFile.getPath()------"+tempFile.getPath());
                       // getIn(tempFile.getPath());

                    } else {
                        Toast.makeText(
                                CameraMain.this,
                                "没有存储卡",
                                Toast.LENGTH_LONG).show();
                    }

                    break;
                case RESULT_REQUEST_CODE:
                    if (data != null) {
                        getImageToView(data);
                    }
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 裁剪图片方法实现
     *
     * @param uri
     */
    public void startPhotoZoom(Uri uri) {

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // 设置裁剪
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 320);
        intent.putExtra("outputY", 320);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, 2);
    }

    /**
     * 保存裁剪之后的图片数据
     *
     * @param picdata
     */
    private void getImageToView(Intent data) {
        Bundle extras = data.getExtras();
        if (extras != null) {
            Bitmap photo = extras.getParcelable("data");
            drawable = new BitmapDrawable(photo);
            round_img.setImageDrawable(drawable);
            Tools.SaveBitmapAsFile(FACE_PATH, photo);
            Log.e("asf", "Face-----"+FACE_PATH);
        }
    }
    //压缩照相
    private void getIn(String path){
        try {

            Bitmap bm = Bimp.revitionImageSize(path);
            int degree = BitmapCache.getBitmapDegree(path);
            if (0 != degree) {

                bm = BitmapCache.rotateBitmapByDegree(bm,
                        degree);
            }
            round_img.setImageBitmap(bm);;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    //压缩相册
    private void getOn(Uri uri){

        ContentResolver cr = this.getContentResolver();
        try {
            Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
            Tools.SaveBitmapAsFile(FACE_PATH, bitmap);
            getIn(FACE_PATH);
             /* 将Bitmap设定到ImageView */

        } catch (FileNotFoundException e) {
            Log.e("Exception", e.getMessage(), e);
        }
    }
}

